package cn.iocoder.yudao.module.exam.service.question;

import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils;
import cn.iocoder.yudao.module.exam.controller.admin.questionoption.vo.QuestionOptionSaveReqVO;
import cn.iocoder.yudao.module.exam.dal.dataobject.bank.BankDO;
import cn.iocoder.yudao.module.exam.dal.mysql.bank.BankMapper;
import cn.iocoder.yudao.module.exam.service.questionoption.QuestionOptionService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.module.exam.controller.admin.question.vo.*;
import cn.iocoder.yudao.module.exam.dal.dataobject.question.QuestionDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.exam.dal.mysql.question.QuestionMapper;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.exam.enums.ErrorCodeConstants.*;

/**
 * 试题 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class QuestionServiceImpl implements QuestionService {

    @Resource
    private QuestionMapper questionMapper;

    @Resource
    private QuestionOptionService questionOptionService;
    @Resource
    private BankMapper bankMapper;

    @Override
    public Long createQuestion(QuestionSaveReqVO createReqVO) {
        // 插入
        QuestionDO question = BeanUtils.toBean(createReqVO, QuestionDO.class);
        questionMapper.insert(question);
        questionOptionService.save(createReqVO.getBankId(),question.getId(),createReqVO.getSelectedOptions());
        // 返回
        return question.getId();
    }

    @Override
    public void updateQuestion(QuestionSaveReqVO updateReqVO) {
        // 校验存在
        validateQuestionExists(updateReqVO.getId());
        // 更新
        QuestionDO updateObj = BeanUtils.toBean(updateReqVO, QuestionDO.class);
        questionMapper.updateById(updateObj);
        questionOptionService.save(updateReqVO.getBankId(),updateReqVO.getId(),updateReqVO.getSelectedOptions());
    }

    @Override
    public void deleteQuestion(Long id) {
        // 校验存在
        validateQuestionExists(id);
        // 删除
        questionMapper.deleteById(id);
    }

    private void validateQuestionExists(Long id) {
        if (questionMapper.selectById(id) == null) {
            throw exception(QUESTION_NOT_EXISTS);
        }
    }

    @Override
    public QuestionRespVO getQuestion(Long id) {
        QuestionDO questionDO = questionMapper.selectById(id);
        QuestionRespVO vo = BeanUtils.toBean(questionDO, QuestionRespVO.class);
        List<QuestionOptionSaveReqVO> options =  questionOptionService.findOptions(questionDO.getBankId(),questionDO.getId());
        vo.setSelectedOptions(options);
        return vo;
    }

    @Override
    public PageResult<QuestionDO> getQuestionPage(QuestionPageReqVO pageReqVO) {
        PageResult<QuestionDO> questionDOPageResult = questionMapper.selectPage(pageReqVO);
        return questionDOPageResult;
    }

    @Override
    public PageResult<QuestionRespVO> getQuestionVoPage(QuestionPageReqVO pageReqVO) {
        IPage<QuestionRespVO> mpPage = MyBatisUtils.buildPage(pageReqVO);
        mpPage.orders().add(OrderItem.desc("exa.id"));
        IPage<QuestionRespVO> listPageResult = questionMapper.selectVoPage(mpPage, pageReqVO);
        for (QuestionRespVO record : listPageResult.getRecords()) {
            List<QuestionOptionSaveReqVO> options =  questionOptionService.findOptions(record.getBankId(),record.getId());
            record.setSelectedOptions(options);
        }
        return new PageResult<>(listPageResult.getRecords(),listPageResult.getTotal());
    }

}